package com.googlecode.cswish.view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.components.UIBean;

import com.googlecode.cswish.model.PageElement;
import com.googlecode.cswish.model.PageElements;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.model.TextModel;
import com.googlecode.cswish.struts.spring.ObjectFactory;
import com.googlecode.cswish.util.BlankObject;
import com.googlecode.cswish.util.FrameConstant;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * Describe the list data - Edit model / Text model
 * 
 * @author Jerry.Feng 2008-10-17
 */
@Deprecated
public class DataGrid extends UIBean {
	private static final Log logger = LogFactory.getLog(DataGrid.class);
	
	final public static String TEMPLATE = "datagrid";
	
	final public static String PAGING_TEMPLATE = "pageInfo";
	
	protected String cascade;
	
	@Resource
	private ObjectFactory objectFactory;

	// current sorted column name
	private String sortable;

	// data source
	private Object items;
	// load the data source from itemName
	private String itemName;
	// the data type of the data source
	private String itemType;
	// displayed columns
	private Collection<Column> columns;
	
	private boolean isPaging;
	// table width
	private String width;
	// table height
	private String height;
	
	private String template = TEMPLATE;

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public DataGrid(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
		super(stack, request, response);
		columns = new ArrayList<Column>();
	}

	@Override
	protected String getDefaultTemplate() {
		return template;
	}
	private Object findData() {
		Object oTextMode = getParameters().get("textModel");
		String itemType = (String) getParameters().get("itemType");
		Object data = DataGridHelper.findData(getItemName(), oTextMode, itemType);
		isPaging = (data instanceof PageInfo);
		return data;
	}
	
	@Override
	public void evaluateExtraParams() {
		if (getSortable() != null) {
			addParameter("sortable", findValue(getSortable()));
		}

		items = findData();
		addParameter("items", items);
		
		if (getWidth() != null) {
			addParameter("width", findValue(getWidth(), String.class));
		}
		if (getHeight() != null) {
			addParameter("height", findValue(getHeight(), String.class));
		}
		if (columns == null || columns.size() <= 0) {
			columns = (Collection<Column>) getParameters().get("columns");
			if (columns == null || columns.size() <= 0) {
				columns = caculateDefaultColumn();
			}
		}
		addParameter("columns", columns);
		
		template = isPaging ? PAGING_TEMPLATE : TEMPLATE;
	}

	/**
	 * Give 'columns' a default value 
	 * 
	 * @return
	 */
	private List<Column> caculateDefaultColumn() {
		// Read the column information from page structure
		PageElements ps = (PageElements) stack.getContext().get(FrameConstant.STACK_PAGEELEMENTS);
		List<Column> columns = new ArrayList<Column>();
//		List<PageElement> list = ps.getElements();
//		for (PageElement c : list) {
//			if (!c.isElement() ) {
//				continue;
//			}
//			
//			Column col = new Column();
//			
//			col.setNamePrefix(itemName);
//			col.setShortName(c.getShortName());
//			col.setProperty(c.getShortName());
//			col.setDisplayProperty((String)c.getExtraParams().get("displayProperty"));
//			
//			if (c.getLabel() == null) {
//				col.setLabel(c.getShortName());
//			} else {
//				col.setLabel(c.getLabel());
//			}
//			col.setType(c.getType());
//			if (c.getHidden() != null && c.getHidden()) {
//				col.setVisible(false);
//			} else {
//				col.setVisible(true);
//			}
//			
//			col.setRequired(c.getRequired() != null && c.getRequired());
//			col.setReadonly(TextModel.TRUE.equals(c.getTextModel()) || TextModel.TRUE_WITH_VALUE.equals(c.getTextModel()));
//			if (c.getLength() != null) {
//				col.setWidth(String.valueOf(c.getLength()));
//			}
//			columns.add(col);
//			
//			col.setRefId(c.getName());
//		}
		return columns;
	}
	
	protected boolean evaluateNameValue() {
        return false;
    }

	public void addColumn(Column e) {
		columns.add(e);
	}

	public void setSortable(String sortable) {
		this.sortable = sortable;
	}

	public String getSortable() {
		return sortable;
	}

	public Object getItems() {
		return items;
	}

	public void setItems(Object items) {
		this.items = items;
	}

	public String getItemName() {
		if (itemName == null) {
			itemName = (String) getParameters().get("itemName");
			
			if (itemName == null) {
				PageElements ps = (PageElements) stack.getContext().get(FrameConstant.STACK_PAGEELEMENTS);
				itemName = ps.getName();
			}
		}
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public String getItemType() {
		return itemType;
	}

	public void setItemType(String itemType) {
		this.itemType = itemType;
	}

	public Collection<Column> getColumns() {
		return columns;
	}

	public void setColumns(Collection<Column> columns) {
		this.columns = columns;
	}
}